package com.lihaogn.sell.web;

import com.lihaogn.sell.entity.Food;
import com.lihaogn.sell.service.ProductService;
import com.lihaogn.sell.vo.FoodVo;
import com.lihaogn.sell.vo.ListVo;
import com.lihaogn.sell.vo.SearchPageVo;
import com.lihaogn.sell.vo.UploadFileVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @FileName: ProductController
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-05-14 10:46
 * @Version: 1.0
 */
@Controller
@Api(tags = "商品数据接口")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    private ListVo listVo;

    @Value("${web.upload-path}")
    String destPath;

    String foodId;

    // ======================= 访问页面 =======================

    @ApiIgnore
    @RequestMapping(value = "/foodShow")
    private String foodShow() {
        return "product/food-list";
    }

    @ApiIgnore
    @RequestMapping(value = "/foodAdd")
    private String foodAdd() {
        return "product/food-add";
    }

    @ApiIgnore
    @RequestMapping(value = "/foodEdit")
    private String foodEdit(String foodId) {
        this.foodId=foodId;
        return "product/food-edit";
    }
    // ======================= 操作 =======================

    // ========== 查询 ==========

    /**
     * 根据条件搜索food
     * @param searchPageVo
     * @return
     */
    @GetMapping(value = "/foodSearch")
    @ResponseBody
    private ListVo foodSearch(SearchPageVo searchPageVo) {


        return productService.searchFood(searchPageVo);
    }

    /**
     * 分页获取food列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "/showAllFood")
    @ResponseBody
    private ListVo showAllFood(String page, String limit) {

        List<FoodVo> foodList = productService.getAllFoodForPage(page, limit);
        listVo.setCode(0);
        listVo.setMsg("success");
        listVo.setCount(productService.getFoodRowsCount());
        listVo.setData(foodList);

        return listVo;

    }

    /**
     * 根据foodName获取food
     * @param foodName
     * @return
     */
    @GetMapping(value = "/queryFoodName")
    @ResponseBody
    private Integer queryFoodName(String foodName) {

        return productService.queryExistFoodName(foodName);
    }

    /**
     * 根据id获取一个food，用于编辑
     * @return
     */
    @GetMapping(value = "/getFoodForEdit")
    @ResponseBody
    private Food getFoodForEdit() {

        return productService.getOneFoodById(foodId);
    }

    // ========== 添加 ==========

    /**
     * 上传图片
     * @param uploadFile
     * @param request
     * @return
     */
    @PostMapping(value = "/uploadImage")
    @ResponseBody
    private UploadFileVo uploadImage(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) {

        // 返回信息
        UploadFileVo uploadFileVo = new UploadFileVo();

        String filename = uploadFile.getOriginalFilename();

        // 创建上传文件夹
        File folder = new File(destPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        // 保存的文件路径
        String filePath = "";
        // 保存文件
        if (filename != null) {
            FileOutputStream outputStream=null;
            // 重命名文件
            String newFileName = UUID.randomUUID().toString() +
                    filename.substring(filename.lastIndexOf("."));
            try {
                // 保存文件
                outputStream = new FileOutputStream(destPath + newFileName);
                outputStream.write(uploadFile.getBytes());
                outputStream.flush();
                outputStream.close();

                filePath = "pic/" + newFileName;

                uploadFileVo.setCode(1);
                uploadFileVo.setMsg("success");
            } catch (IOException e) {
                e.printStackTrace();
                uploadFileVo.setCode(0);
                uploadFileVo.setMsg("fail");
            }
            uploadFileVo.setData(filePath);

            return uploadFileVo;
        }
        uploadFileVo.setCode(0);
        uploadFileVo.setMsg("fail");
        uploadFileVo.setData(filePath);

        return uploadFileVo;
    }

    /**
     * 添加一个food
     *
     * @param food
     * @return
     */
    @PostMapping(value = "/addFood")
    @ResponseBody
    private Integer addFood(Food food) {
        food.setFoodId(UUID.randomUUID().toString());

        return productService.addOneFood(food);
    }

    // ========== 更新 ==========

    @PutMapping(value = "/changeFoodStatus")
    @ResponseBody
    private Integer changeFoodStatus(Food food) {
        int oldStasus = food.getFoodStatus();
        int newStatus;

        if (oldStasus == 0) {
            newStatus = 1;
        } else {
            newStatus = 0;
        }

        food.setFoodStatus(newStatus);

        return productService.updateOneFood(food);
    }

    @PutMapping(value = "/editFood")
    @ResponseBody
    private Integer editFood(Food food) {

        return productService.updateOneFood(food);
    }
    // ========== 删除 ==========

    /**
     * 删除单个food
     * @param foodId
     * @return
     */
    @DeleteMapping(value = "/deleteFood")
    @ResponseBody
    private Integer deleteFood(String foodId) {
        return productService.deleteOneFood(foodId);
    }

    @DeleteMapping(value = "/deleteMultiFood")
    @ResponseBody
    private Integer deleteMultiFood(HttpServletRequest request) {
        return productService.deleteMultiFood(request);
    }

}
